package com.neusoft.databus.common.model;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.OneToMany;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;

import org.hibernate.validator.constraints.Length;

import com.neusoft.bizcore.web.jpa.model.AbstractAuditModel;
import com.neusoft.databus.common.enums.PhysicalConnType;
import com.neusoft.databus.common.enums.ResourceWorkMode;

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

/**
 * 网关实体
 *
 * @author naxiang
 */
@Getter
@Setter
@ToString
@Entity
public class Gateway extends AbstractAuditModel {

    private static final long serialVersionUID = 1L;

    /** 网关编码唯一标识,根据ID生成器生成 */
    @NotBlank(message = "网关编码不能为空")
    @Length(max = 255, message = "网关编码不能超过{max}位")
    @Column(length = 255, nullable = false, unique = true)
    private String code;

    /** 网关名称 */
    @NotBlank(message = "网关名称不能为空")
    @Length(max = 255, message = "网关名称不能超过{max}位")
    @Column(length = 255, nullable = false)
    private String name;

    /** 网关连接方式 */
    @Enumerated(EnumType.STRING)
    @NotNull(message = " 网关连接方式不能为空")
    @Column(nullable = false)
    private PhysicalConnType physicalConnType;

    /** 网关连接信息 */
    @NotBlank(message = "网关连接信息不能为空")
    @Length(max = 1023, message = "网关连接信息不能超过{max}位")
    @Column(length = 1023, nullable = false)
    // @Type(type = "StringJsonObject")
    private String config;

    /** 网关工作模式 */
    @Enumerated(EnumType.STRING)
    @NotNull(message = " 网关工作模式不能为空")
    @Column(nullable = false)
    private ResourceWorkMode workMode = ResourceWorkMode.PULL;

    /** 建立连接的超时时间（秒），默认2秒 */
    @Column(nullable = false)
    public int connectTimeout = 3;

    /** 连接失败后的重试次数，默认不重试 */
    @Column(nullable = false)
    public int connectRetry = 1;

    /** 禁用标识 */
    private boolean disabled = false;

    /** 关联资源 */
    @OneToMany(mappedBy = "gateway", cascade = {CascadeType.MERGE, CascadeType.PERSIST })
    private List<Resource> resources = new ArrayList<>();

    /** 注释 */
    @Length(max = 1023, message = "注释不能超过{max}位")
    @Column(length = 1023, nullable = true)
    private String comment;

    /** 关联资源是否批量采集 */
    private boolean batched = false;

    /** 集群节点ID */
    private int node = 0;
}
